#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ReadDataById_0x22_test.py
@Time    :   2022/08/31 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from ast import If
# from lib2to3.pgen2 import driver
from pickle import FALSE
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
import Set_up
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

#   ('22 F1 86','','123')  已经自动在会话模式切换里进行测试

DID = [ 
        ('22 F1 80', Set_up.VehicleManufacturerECUBootSoftwareVersionNumber),                         # 底层软件Bootload版本号
        ('22 F1 93', [0x62,0xF1, 0x93] + Set_up.ECUHardwareVersionNumber),                                   # ECUHardwareVersionNumber 硬件版本号
        ('22 F1 94', '62F194' + Set_up.ECUSoftwareCode),                                              # ECUSoftwareCode 软件编码
        ('22 F1 95', [0x62,0xF1,0x95] + Set_up.ECUSoftwareVersionNumber),                             # ECUSoftwareVersionNumber 软件版本号
        ('22 34 01', '62 34 01 00'),                                                                  # FactoryMode 生产模式
        ('22 21 00', '62 21 00 11 a6 70 03 00 00 00 7f'),                                              # Variant Coding 配置字
        ('22 F1 90', '62 F1 90 00 00 31 32 33 34 35 36 37 38 39 00 00 00 00 00 00'),                  # VehicleIdentification(VIN) 车辆识别码
        ('22 02 8D', '62 02 8D'),                                                                     # ECUTemperature ECUT温度 
        ('22 10 11', '62 10 11 0B'),                                                                  # VehicleVelocity 车辆速度
        ('22 01 03', '62 01 03 00'),                                                                  # 安全访问次数
        ('22 34 04', '62 34 04'),                                                                     # Fast Horizontal Misalignment Angle 快速水平偏差角
        ('22 34 03', '62 34 03'),                                                                     # Slow Horizontal Misalignment Angle 慢速水平偏差角
        ('22 34 41', '62 34 41'),                                                                     # Fast Vertical Misalignment Angle 快速垂直偏差角
        ('22 34 42', '62 34 42'),                                                                     # Slow Vertical Misalignment Angle
        ]                               


DID_Programming = [ ('22 F1 80', Set_up.VehicleManufacturerECUBootSoftwareVersionNumber, '2'),    # VehicleManufacturerECUBootSoftwareVersionNumber 底层软件Bootload版本号
                    # ECUHardwareVersionNumber 硬件版本号
                    ('22 F1 93', [0x62,0xF1, 0x93] + Set_up.ECUHardwareVersionNumber, '2'),
                    # ECUSoftwareCode 软件编码
                    ('22 F1 94', '62F194' + Set_up.ECUSoftwareCode, '2'),
                    # ECUSoftwareVersionNumber 软件版本号
                    ('22 F1 95', [0x62,0xF1,0x95] + Set_up.ECUSoftwareVersionNumber, '2')]

DID_NRC13 = [('22', '7F 22 13'),
             ('22 F1', '7F 22 13'),
             ('22 10', '7F 22 13'),
             ('22 F1 80 01', '7F 22 13'),
             ('22 10 11 01', '7F 22 13'),
             ('22 01 03 01', '7F 22 13'),
             ('22 02', '7F 22 13'),
             ('22 02 8D 01', '7F 22 13'),
             ('22 F1 90 01', '7F 22 13'),
             ('22 F1 94 01', '7F 22 13'),
             ('22 F1 95 FF', '7F 22 13'),
             ('22 34', '7F 22 13')
             ]

DID_NRC31 = [('22 34 4A', '7F 22 31'),
             ('22 F1 A1', '7F 22 31'),
            ]


class Test_ReadDataById_Phy(object):

    def ReadDID(self,Driver, req, resp, session):
        iStep = 1
        if req == '22 10 11':
        # 设置车速>3kph
            Driver['CAN'].write_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd', 40)
            time.sleep(0.5)
            msg = Driver['CAN'].read_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd')
            logging.info('speed is:{0}'.format(msg))
            time.sleep(0.5)
        if req == '22 F1 90':
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E F1 90 00 00 31 32 33 34 35 36 37 38 39 00 00 00 00 00 00", '6E F1 90', 3, iStep, True)
        if req == '22 21 00':
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2e f1 a1 ff ff ff ff ff ff ff ff", '6e f1 a1', 3, iStep, True)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2e 21 00 11 A6 70 03 00 00 00 7F", '6e 21 00', 0, iStep, False)
        
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep)
        if req == '22 10 11':
            with allure.step(f'Step{iStep}: 判断车速是否正常 # '):
                with assume: assert  uds_resp[4] > 0x1A and uds_resp[3] < 0x1e
                iStep = iStep + 1

            # 设置车速=0kph
            Driver['CAN'].write_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd', 0)
            time.sleep(0.5)
            msg = Driver['CAN'].read_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd')
            logging.info('speed is:{0}'.format(msg))
            time.sleep(0.5)

    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22 in default session with physical addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_Default_Phy(self, action, Driver, req, resp):
        self.ReadDID(Driver, req, resp, 1)

        

    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22 in extended session with physical addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_Extended_Phy(self, action, Driver, req, resp):
        self.ReadDID(Driver, req, resp, 3)


    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22 in Programming session with physical addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_NRC7F_Programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '7F 22 7F', 2, iStep, False)

    

    ids = [f'{req}' for req, resp in DID_NRC13]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC13 in default session with physical addressing')
    @allure.description('Test 0x22 NRC13 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC13, ids=ids)
    def test_ReadDataById_NRC13_Default_phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep)



    ids = [f'{req}' for req, resp in DID_NRC13]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC13 in extended session with physical addressing')
    @allure.description('Test 0x22 NRC13 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC13, ids=ids)
    def test_ReadDataById_NRC13_Extended_phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

    @allure.severity('normal')
    @allure.title('Test 0x02 0x85 of read data by identifier 0x22 in default session with physical addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ReadDataById_0285_Default_Phy(self, action, Driver):
        time.sleep(1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 02 85", '62 02 85', 1, iStep, False)
        with assume:
            # 电压值有误差，所以修改为范围判断
            assert uds_resp[3] > 130 and uds_resp[3] < 150 

    @allure.severity('normal')
    @allure.title('Test 0x02 0x85 of read data by identifier 0x22 in extended session with physical addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ReadDataById_0285_Extended_Phy(self, action, Driver):
        time.sleep(1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 02 85", '62 02 85', 3, iStep, False)
        with assume:
            # 电压值有误差，所以修改为范围判断
            assert uds_resp[3] > 130 and uds_resp[3] < 150

    @allure.severity('normal')
    @allure.title('Test 0x02 0x85 of read data by identifier 0x22 in programming session with physical addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ReadDataById_0285_Programming_Phy(self, action, Driver):
        time.sleep(1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 02 85", '7F 22 7F', 2, iStep, False)


    ids = [f'{req}' for req, resp in DID_NRC31]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC31 in default session with physical addressing')
    @allure.description('Test 0x22 NRC31 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC31, ids=ids)
    def test_ReadDataById_NRC31_Default_phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)


    ids = [f'{req}' for req, resp in DID_NRC31]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC31 in extended session with physical addressing')
    @allure.description('Test 0x22 NRC31 in extended session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC31, ids=ids)
    def test_ReadDataById_NRC31_Extended_phy(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)



class Test_ReadDataById_Fun(object):

    def ReadDID(self,Driver, req, resp, session):
        iStep = 1
        if req == '22 10 11':
        # 设置车速>3kph
            Driver['CAN'].write_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd', 40)
            time.sleep(0.5)
            msg = Driver['CAN'].read_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd')
            logging.info('speed is:{0}'.format(msg))
            time.sleep(0.5)
        if req == '22 F1 90':
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E F1 90 00 00 31 32 33 34 35 36 37 38 39 00 00 00 00 00 00", '6E F1 90', 3, iStep, True, )
        if req == '22 21 00':
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2e f1 a1 ff ff ff ff ff ff ff ff", '6e f1 a1', 3, iStep, True)
            iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2e 21 00 11 A6 70 03 00 00 00 7F", '6e 21 00', 0, iStep, False)
        
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, session, iStep, False,'UDSFun')
        if req == '22 10 11':
            with allure.step(f'Step{iStep}: 判断车速是否正常 # '):
                with assume: assert  uds_resp[4] > 0x1A and uds_resp[3] < 0x1e
                iStep = iStep + 1

            # 设置车速=0kph
            Driver['CAN'].write_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd', 0)
            time.sleep(0.5)
            msg = Driver['CAN'].read_db_signal('CAKY_FRM', 'BCM', 'abs_sts_run1', 'abs_pm_veh_spd')
            logging.info('speed is:{0}'.format(msg))
            time.sleep(0.5)

    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22 in default session with functional addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_Default_Fun(self, action, Driver, req, resp):
        self.ReadDID(Driver, req, resp, 1)

        

    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22 in extended session with functional addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_Extended_Fun(self, action, Driver, req, resp):
        self.ReadDID(Driver, req, resp, 3)


    ids = [f'{req}' for req, resp in DID]
    @allure.severity('normal')
    @allure.title('Test {req} of read data by identifier 0x22 in Programming session with functional addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID, ids=ids)
    def test_ReadDataById_NRC7F_Programming_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, '7F 22 7F', 2, iStep, False, 'UDSFun')

    

    ids = [f'{req}' for req, resp in DID_NRC13]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC13 in default session with functional addressing')
    @allure.description('Test 0x22 NRC13 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC13, ids=ids)
    def test_ReadDataById_NRC13_Default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun')



    ids = [f'{req}' for req, resp in DID_NRC13]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC13 in extended session with functional addressing')
    @allure.description('Test 0x22 NRC13 in extended session with functional addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC13, ids=ids)
    def test_ReadDataById_NRC13_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun')

    @allure.severity('normal')
    @allure.title('Test 0x02 0x85 of read data by identifier 0x22 in default session with functional addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ReadDataById_0285_Default_Fun(self, action, Driver):
        time.sleep(1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 02 85", '62 02 85', 1, iStep, False, 'UDSFun')
        with assume:
            # 电压值有误差，所以修改为范围判断
            assert uds_resp[3] > 130 and uds_resp[3] < 150 

    @allure.severity('normal')
    @allure.title('Test 0x02 0x85 of read data by identifier 0x22 in extended session with functional addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ReadDataById_0285_Extended_Fun(self, action, Driver):
        time.sleep(1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 02 85", '62 02 85', 3, iStep, False, 'UDSFun')
        with assume:
            # 电压值有误差，所以修改为范围判断
            assert uds_resp[3] > 130 and uds_resp[3] < 150

    @allure.severity('normal')
    @allure.title('Test 0x02 0x85 of read data by identifier 0x22 in programming session with functional addressing')
    @allure.story('Read data by identifier test')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    def test_ReadDataById_0285_Programming_Fun(self, action, Driver):
        time.sleep(1)
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "22 02 85", '7F 22 7F', 2, iStep, False, 'UDSFun')


    ids = [f'{req}' for req, resp in DID_NRC31]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC31 in default session with functional addressing')
    @allure.description('Test 0x22 NRC31 in default session with functional addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC31, ids=ids)
    def test_ReadDataById_NRC31_Default_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDSFun')


    ids = [f'{req}' for req, resp in DID_NRC31]
    @allure.severity('normal')
    @allure.title('Test 0x22 NRC31 in extended session with functional addressing')
    @allure.description('Test 0x22 NRC31 in extended session with functional addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
    @pytest.mark.parametrize('req, resp', DID_NRC31, ids=ids)
    def test_ReadDataById_NRC31_Extended_Fun(self, action, Driver, req, resp):
        iStep = 1
        iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDSFun')

